﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StockholmsStad.Web.UI.Class.Utilities;

namespace StockholmsStad.Web.UI.Test
{
    /// <summary>
    ///This is a test class for DateTimeUtilTest and is intended
    ///to contain all DateTimeUtilTest Unit Tests
    ///</summary>
    [TestClass]
    public class DateTimeUtilTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        /// <summary>
        ///A test for GetFirstYearAndDayMatch
        ///</summary>
        [TestMethod]
        public void GetFirstYearAndDayMatchTest()
        {
            var date = new DateTime(2008, 01, 01);
            IList<DateTime> list = new List<DateTime>
                                       {
                                           date,
                                           new DateTime(2008, 02, 02),
                                           new DateTime(2008, 01, 01, 14, 15, 16),
                                       };
            Assert.AreEqual(date, DateTimeUtil.GetFirstYearAndDayMatch(date, list).Value);
        }

        /// <summary>
        ///A test for GetFirstYearAndDayMatch
        ///</summary>
        [TestMethod]
        public void GetFirstYearAndDayMatchTest2()
        {
            var date2 = new DateTime(2008, 04, 01, 15, 15, 15);
            var date3 = new DateTime(2008, 04, 01, 16, 16, 16);
            IList<DateTime> list2 = new List<DateTime>
                                        {
                                            date2,
                                            new DateTime(2008, 02, 02),
                                            new DateTime(2008, 01, 01, 14, 15, 16),
                                        };
            Assert.AreEqual(date2, DateTimeUtil.GetFirstYearAndDayMatch(date3, list2));
        }

        /// <summary>
        ///A test for GetFirstYearAndDayMatch
        ///</summary>
        [TestMethod]
        public void GetFirstYearAndDayMatchTest3()
        {
            var date2 = new DateTime(2008, 04, 01, 15, 15, 15);
            IList<DateTime> list2 = new List<DateTime>
                                        {
                                            date2,
                                            new DateTime(2008, 02, 02),
                                            new DateTime(2008, 01, 01, 14, 15, 16),
                                        };
            var date4 = new DateTime(2009, 01, 01);
            Assert.IsNull(DateTimeUtil.GetFirstYearAndDayMatch(date4, list2));
        }

        /// <summary>
        ///A test for GetFirstYearAndDayMatch
        ///</summary>
        [TestMethod]
        public void GetFirstYearAndDayMatchTest4()
        {
            IList<DateTime> list2 = new List<DateTime>
                                        {
                                        };
            var date4 = new DateTime(2009, 01, 01);
            Assert.IsNull(DateTimeUtil.GetFirstYearAndDayMatch(date4, list2));
        }

        /// <summary>
        ///A test for GetFirstTenListItems
        ///</summary>
        [TestMethod]
        public void GetFirstTenListItemsTest()
        {
            var date1 = new DateTime(2008, 01, 01, 14, 15, 16);
            var date2 = new DateTime(2008, 01, 01, 14, 16, 17);
            var date3 = new DateTime(2008, 03, 01, 14, 15, 16);
            var date4 = new DateTime(2008, 04, 01, 14, 15, 17);
            var date5 = new DateTime(2008, 04, 02, 14, 15, 17);
            var date6 = new DateTime(2008, 05, 01, 14, 15, 16);
            var date7 = new DateTime(2008, 05, 02, 14, 15, 16);
            var date8 = new DateTime(2008, 06, 01, 14, 15, 17);
            var date9 = new DateTime(2008, 06, 02, 14, 15, 17);
            var date10 = new DateTime(2008, 07, 01, 14, 15, 16);
            IList<DateTime> list = new List<DateTime>
                                       {
                                           new DateTime(2009, 02, 02),
                                           date1,
                                           date2,
                                           date3,
                                           date4,
                                           date6,
                                           date8,
                                           date10,
                                           new DateTime(2008, 08, 01, 14, 15, 17),
                                           new DateTime(2008, 09, 01, 14, 15, 16),
                                           date5,
                                           date7,
                                           date9,
                                       };
            var expected = new ListItem[]
                               {
                                   new ListItem("2008-01-01  14:15", date1.Ticks.ToString()),
                                   new ListItem("2008-01-01  14:16", date2.Ticks.ToString()),
                                   new ListItem("2008-03-01  14:15", date3.Ticks.ToString()),
                                   new ListItem("2008-04-01  14:15", date4.Ticks.ToString()),
                                   new ListItem("2008-04-02  14:15", date5.Ticks.ToString()),
                                   new ListItem("2008-05-01  14:15", date6.Ticks.ToString()),
                                   new ListItem("2008-05-02  14:15", date7.Ticks.ToString()),
                                   new ListItem("2008-06-01  14:15", date8.Ticks.ToString()),
                                   new ListItem("2008-06-02  14:15", date9.Ticks.ToString()),
                                   new ListItem("2008-07-01  14:15", date10.Ticks.ToString()),
                               };
            ListItem[] actual;
            actual = DateTimeUtil.GetFirstListItems(10, list, GetDateTimeText);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i].Text, actual[i].Text);
                Assert.AreEqual(expected[i].Value, actual[i].Value);
            }
            Assert.AreEqual(expected.Length, actual.Length);
        }

        private static string GetDateTimeText(DateTime date)
        {
            return date.ToString("yyyy-MM-dd  HH:mm");
        }

        /// <summary>
        ///A test for GetFirstTenListItems
        ///</summary>
        [TestMethod]
        public void GetFirstTenListItemsTest2()
        {
            var date1 = new DateTime(2008, 01, 01, 14, 15, 16);
            var date2 = new DateTime(2008, 01, 01, 14, 16, 17);
            var date3 = new DateTime(2008, 03, 01, 14, 15, 16);
            var date4 = new DateTime(2008, 04, 01, 14, 15, 17);
            var date5 = new DateTime(2008, 04, 02, 14, 15, 17);
            IList<DateTime> list = new List<DateTime>
                                       {
                                           date1,
                                           date2,
                                           date3,
                                           date4,
                                           date5,
                                       };
            var expected = new[]
                               {
                                   new ListItem("2008-01-01  14:15", date1.Ticks.ToString()),
                                   new ListItem("2008-01-01  14:16", date2.Ticks.ToString()),
                                   new ListItem("2008-03-01  14:15", date3.Ticks.ToString()),
                                   new ListItem("2008-04-01  14:15", date4.Ticks.ToString()),
                                   new ListItem("2008-04-02  14:15", date5.Ticks.ToString()),
                               };
            ListItem[] actual;
            actual = DateTimeUtil.GetFirstListItems(10, list, GetDateTimeText);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i].Text, actual[i].Text);
                Assert.AreEqual(expected[i].Value, actual[i].Value);
            }
            Assert.AreEqual(expected.Length, actual.Length);
        }

        /// <summary>
        ///A test for GetFirstTenListItems
        ///</summary>
        [TestMethod]
        public void GetFirstTenListItemsTest3()
        {
            IList<DateTime> list = new List<DateTime>
                                       {
                                       };
            var expected = new ListItem[]
                               {
                               };
            ListItem[] actual;
            actual = DateTimeUtil.GetFirstListItems(10, list, GetDateTimeText);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i].Text, actual[i].Text);
                Assert.AreEqual(expected[i].Value, actual[i].Value);
            }
            Assert.AreEqual(expected.Length, actual.Length);
        }

        /// <summary>
        ///A test for GetEarliestDate
        ///</summary>
        [TestMethod]
        public void GetEarliestDateTest()
        {
            var date1 = new DateTime(2008, 01, 01, 14, 15, 16);
            var date2 = new DateTime(2008, 01, 01, 14, 16, 17);
            var date3 = new DateTime(2008, 03, 01, 14, 15, 16);
            var date4 = new DateTime(2008, 04, 01, 14, 15, 17);
            var date5 = new DateTime(2008, 04, 02, 14, 15, 17);
            var date6 = new DateTime(2008, 05, 01, 14, 15, 16);
            var date7 = new DateTime(2008, 05, 02, 14, 15, 16);
            var date8 = new DateTime(2008, 06, 01, 14, 15, 17);
            var date9 = new DateTime(2008, 06, 02, 14, 15, 17);
            var date10 = new DateTime(2008, 07, 01, 14, 15, 16);
            IList<DateTime> list = new List<DateTime>
                                       {
                                           new DateTime(2009, 02, 02),
                                           date1,
                                           date2,
                                           date3,
                                           date4,
                                           date6,
                                           date8,
                                           date10,
                                           new DateTime(2008, 08, 01, 14, 15, 17),
                                           new DateTime(2008, 09, 01, 14, 15, 16),
                                           date5,
                                           date7,
                                           date9,
                                       };
            IEnumerable<DateTime> dates = list;
            DateTime expected = date1;
            DateTime? actual;
            actual = DateTimeUtil.GetEarliestDate(dates);
            Assert.AreEqual(expected, actual.Value);
        }

        /// <summary>
        ///A test for GetEarliestDate
        ///</summary>
        [TestMethod]
        public void GetEarliestDateTest2()
        {
            IList<DateTime> list = new List<DateTime>
                                       {
                                       };
            IEnumerable<DateTime> dates = list;
            DateTime? expected = null;
            DateTime? actual;
            actual = DateTimeUtil.GetEarliestDate(dates);
            Assert.AreEqual(expected, actual);
        }
    }
}